package com.swak.frame.excel.builder;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.handler.WriteHandler;
import com.swak.frame.excel.AutoColumnWidthStyleStrategy;
import com.swak.frame.excel.DataValidationCellWriteHandler;
import com.swak.frame.excel.DynamicCustomerHandler;
import com.swak.frame.excel.converter.BooleanStrConverter;
import com.swak.frame.excel.metadata.ExcelWriteData;
import com.swak.frame.excel.metadata.WriteExcelParams;
import com.swak.frame.util.StringPool;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

public class SwakExcelWriterBuilder extends ExcelWriterBuilder {

	public static SwakExcelWriterBuilder write(OutputStream outputStream) {
		return write(outputStream, null);
	}

	public static SwakExcelWriterBuilder write(OutputStream outputStream, Class<?> head) {
		SwakExcelWriterBuilder excelWriterBuilder = new SwakExcelWriterBuilder();
		excelWriterBuilder.file(outputStream);
		if (head != null) {
			excelWriterBuilder.head(head);
		}
		return excelWriterBuilder;
	}


	@Override
    public SwakExcelWriterBuilder registerConverter(Converter<?> converter) {
		super.registerConverter(converter);
		return this;
	}

	public SwakExcelWriterBuilder registerConverter(List<Converter<?>> converters) {
		if (CollectionUtils.isNotEmpty(converters)) {
			for (Converter<?> converter : converters) {
				super.registerConverter(converter);
			}
		}
		return this;
	}

	public SwakExcelWriterBuilder dynamicTitleHeader(Map<String,String> dynamicHeader) {
		super.registerWriteHandler(new DynamicCustomerHandler(dynamicHeader));
		return this;
	}

	@Override
    public SwakExcelWriterBuilder registerWriteHandler(WriteHandler writeHandler) {
		super.registerWriteHandler(writeHandler);
		return this;
	}

	public SwakExcelWriterBuilder registerWriteHandler(List<WriteHandler> writeHandlers) {
		if (CollectionUtils.isNotEmpty(writeHandlers)) {
			for (WriteHandler writeHandler : writeHandlers) {
				super.registerWriteHandler(writeHandler);
			}
		}
		return this;
	}

	public SwakExcelWriterBuilder defaultWriteHandler(WriteExcelParams writeParams) {
		if (StringUtils.isNoneBlank(writeParams.getTemplatePath())) {
			super.withTemplate(writeParams.getTemplatePath());
		}
		super.autoCloseStream(Boolean.TRUE).charset(Charset.forName(StringPool.UTF8))
				.password(writeParams.getPassword()).excelType(writeParams.getExcelType());
		super.registerWriteHandler(AutoColumnWidthStyleStrategy.createStyleStrategy(writeParams));
		if(writeParams.isAutoColumnWidth()) {
			super.registerWriteHandler(new AutoColumnWidthStyleStrategy());
		}
		super.registerWriteHandler(new AutoColumnWidthStyleStrategy.CustomRowHeightStyleStrategy(writeParams));
		defaultRegisterConverter();
		return this;
	}

	public SwakExcelWriterBuilder defaultWriteHandler(WriteExcelParams writeParams, ExcelWriteData<?> excelWriteData) {
		defaultWriteHandler(writeParams).registerWriteHandler(new DataValidationCellWriteHandler(excelWriteData));
		return this;
	}

	public SwakExcelWriterBuilder defaultRegisterConverter() {
		registerConverter(BooleanStrConverter.getConverter());
		return this;
	}
}
